<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\User;
use App\Models\Number;
use Carbon\Carbon;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class UserController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(User::with('number'), function (Grid $grid) {
            $type = request()->get('type');

            if ($type) {
                switch ($type) {
                    case '1':
                        $grid->model()->where('days_remaining', '>', 7);

                        break;
                    case '2':
                        $grid->model()->where('days_remaining', '<', 7);
                        $grid->model()->where('days_remaining', '>', 0);

                        break;
                    case '3':
                        $grid->model()->where('days_remaining', '<', 0);

                        break;
                }
            }

            $grid->column('id')->sortable();
            $grid->column('type')->using(admin_trans('user.options.type'));
            $grid->column('number.number', admin_trans('user.fields.number_id'));
            $grid->column('name');
            $grid->column('start_at');
            $grid->column('end_at');
            $grid->column('days_remaining')->sortable()->display(function ($days_remaining) {
                if ($days_remaining <= 7 && $days_remaining > 0) {
                    return '<div style="background: #42A5F5;border-radius: 5px;text-align: center;color: #fff;font-size: 16px;font-weight: 600;">'.$days_remaining.'</div>';
                } elseif ($days_remaining <= 0) {
                    return '<div style="background: #E53935;border-radius: 5px;text-align: center;color: #fff;font-size: 16px;font-weight: 600;">'.$days_remaining.'</div>';
                } else {
                    return $days_remaining;
                }
            });

            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->like('name');
                $filter->equal('type')->select(admin_trans('user.options.type'));
                $filter->date('start_at');
                $filter->date('end_at');
            });

            $grid->addTableClass(['table-text-center']);
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new User(), function (Show $show) {
            $show->field('id');
            $show->field('number');
            $show->field('name');
            $show->field('phone');
            $show->field('id_card');
            $show->field('start_at');
            $show->field('end_at');
            $show->field('days_remaining');
            $show->field('type');
            $show->field('createt_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new User(), function (Form $form) {
            $number_id = request()->get('number_id') ?? '';

            $form->display('id');

            $form->select('number_id')->options(Number::pluck('number', 'id'))->value($number_id)->required();
            $form->text('name')->required();
//            $form->text('phone');
//            $form->text('id_card');
            $form->datetime('start_at')->required();
            $form->datetime('end_at')->required();

            $form->display('created_at');
            $form->display('updated_at');
            $form->hidden('type');
            $form->hidden('days_remaining');

            $form->saving(function (Form $form) {
                if (date('Y-m-d H:i:s') > $form->start_at) {
                    $datetime1 = time();
                } else {
                    $datetime1 = strtotime($form->start_at);
                }

                $datetime2 = strtotime($form->end_at);
                $diff = $datetime2 - $datetime1;

                $interval = floor($diff / (60 * 60 * 24));

                $number = Number::find($form->number_id);

                $user = \App\Models\User::where('number_id', $form->number_id)->first();

                if ($user && $user->id != $form->model()->id) {
                    return $form->response()->error('该编号已被使用！');
                }

                $form->type = $number->type;
                $form->days_remaining = $interval;
            });
        });

//        $number = 19;
//
//        $type = 7;
//        $type2 = 14;
//
//        $n = 70;
//
//        for ($i = 1; $i <= $number; $i ++) {
//            Number::where('number', 'D-'.sprintf("%02d", $n).'-'.sprintf("%02d", $i))->update([
//                'type' => $type,
//            ]);
//            Number::where('number', 'G-'.sprintf("%02d", $n).'-'.sprintf("%02d", $i))->update([
//                'type' => $type2
//            ]);
//        }
//        dd('执行完成');
    }
}
